<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Itmontenegro extends CI_Controller {

        private $data = array();
        private $syslang;
        private $fields = array('post_title','content');

        public function __construct(){

            parent::__construct();

            $this->data['url'] = base_url();
            $this->syslang = $this->lang->lang();
            $this->data['local_lang'] = $this->syslang;

            $this->load->helper('language');
            $this->load->config('pages');
            // load language file
            $this->lang->load('it-montenengro');

            /* Translate Class */
            $this->load->library('translate',array('lang'=>$this->lang->lang()));

            //urlify library for prety url
            $linktoclass = "urlify.php";
            require_once($linktoclass);

        }

        public function index()
        { 
            redirect(base_url('home'));
        }

        public function get_page(){

            $this->data['title'] = 'Welcome';
            $this->data['page'] = 'home';


            $this->data['home'] = $this->db->get_where('posts',array('ID'=>45))->row_array();
            $this->data['rentacar'] = $this->db->get_where('posts',array('ID'=>46))->row_array();
            $this->data['excursions'] = $this->db->get_where('posts',array('ID'=>47))->row_array();
            $this->data['accommodations'] = $this->db->get_where('posts',array('ID'=>57))->row_array();
            $this->data['tickerNews'] = $this->db->get_where('posts',array('post_type'=>'news'))->result_array();
            //print_r($this->data['tickerNews']);

            // Translate
            $this->data['home'] = $this->translate->trans_row_array($this->data['home']);
            $this->data['rentacar'] = $this->translate->trans_row_array($this->data['rentacar']);
            $this->data['excursions'] = $this->translate->trans_row_array($this->data['excursions']);
            $this->data['accommodations'] = $this->translate->trans_row_array($this->data['accommodations']);
            $this->data['tickerNews'] = $this->translate->trans_res_array($this->data['tickerNews']);

            $this->template->build('home/index', $this->data);

        }

        public function get_platform($platform){

            switch($platform){

                case 'rentacar':
                    $this->data['title'] = 'Rentcar PRO';
                    $this->data['page'] = 'rentacar'; 
                    $id=46; 
                    break;
                case 'excursions':
                    $this->data['title'] = 'Excursions &amp; Tours PRO';
                    $this->data['page'] = 'excursions';
                    $id=47;  
                    break;
                case 'accommodations':
                    $this->data['title'] = 'Accommodations PRO';
                    $this->data['page'] = 'accommodations';
                    $id=57; 
                    break;
                case 'cms':
                    $this->data['title'] = 'CMS PRO';
                    $this->data['page'] = 'cms';
                    $id=53; 
                    break;

            }

            $q="
            SELECT posts.*, gallery.path as path_gallery, gallery.ID as ID_gallery
            FROM posts
            LEFT JOIN gallery ON gallery.posts_ID = posts.ID
            WHERE posts.ID = $id
            ";

            $this->data['platform'] = $this->db->query($q)->row_array();

            // Translate
            $this->data['platform'] = $this->trans_row_array($this->data['platform']);

            $this->db->order_by('order asc, ID asc');
            $this->data['pictures'] = $this->db->get_where('pictures', array('gallery_ID'=>$this->data['platform']['ID_gallery']))->result_array();

            //echo 'GALLERY ID:'.$this->data['platform']['ID_gallery']."<br /><br />";
            //print_r($this->data['pictures']);

            $this->template->build('platforms/index', $this->data);

        }

        public function get_terms(){

            $this->data['title'] = 'Terms of Business';
            $this->data['page'] = 'terms'; 

            $this->data['terms'] = $this->db->get_where('posts', array('ID'=>50))->row_array();

            // Translate
            $this->data['terms'] = $this->trans_row_array($this->data['terms']);

            $this->template->build('terms/index', $this->data);
        }              

        private function trans_row_array($row){
            if(count($row)>0){

                //Translate class
                foreach($this->fields as $f):

                    $row[$f] = $this->translate->getArray($row[$f], TRUE);
                    if($row[$f]=='')$row[$f]='-Please translate-';

                    endforeach;

                RETURN $row; 

            }
            RETURN "";
        }

        function notification_mail(){

            $msg = $this->load->view('platforms/notification_email', $_POST, TRUE);
            
            $success = false;

            $success = $this->mail_send('ikukic@yahoo.com',$msg);

            if($success) {
                echo json_encode(array('action'=>true,'msg'=>'Succefull. Thank you for contacting us.'));
            }else{
                echo json_encode(array('action'=>false,'msg'=>'Something wrong. Please try again'));
            }

        }

        function mail_send($to,$message){

            $from = 'no-reply@it-montenegro.me';
            $subject = 'Notification Email - IT Montenegro';

            $headers  = "From: $from\r\n";
            $headers .= "Content-type: text/html\r\n";

            return mail($to, $subject, $message, $headers); //0 or 1

        }

    }

    /* End of file itmontenegro.php */
    /* Location: ./application/controllers/itmontenegro.php */